package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbGoods;
import com.shopping.entity.TbOrder;
import com.shopping.entity.TbOrderDetail;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class OrderServlet
 */
public class OrderServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OrderServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see Servlet#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        BizDaoImpl bdi = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bdi);
        TbOrder order = new TbOrder();

        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        String number = request.getParameter("number").trim();
        order.setNumber(number);
        order.setName(request.getParameter("name"));
        order.setReallyName(request.getParameter("reallyName"));
        order.setAddress(request.getParameter("address"));
        order.setTel(request.getParameter("tel"));
        order.setSetMoney(request.getParameter("setMoney"));
        order.setPost(request.getParameter("post"));
        order.setBz(request.getParameter("bz"));
        order.setSign("0");
        boolean flag = bsi.insertOrder(order);
        session.setAttribute("number", number);
        HashMap<Integer, TbGoods> map = (HashMap<Integer, TbGoods>) session.getAttribute("map");
        Set<Integer> set = map.keySet();
        for (Integer integer : set) {
            TbOrderDetail orderdetail = new TbOrderDetail();
            orderdetail.setOrderNumber(request.getParameter("number").trim());
            orderdetail.setGoodsId(map.get(integer).getId());
            if (map.get(integer).getFreePrice() > 0) {
                orderdetail.setPrice(map.get(integer).getFreePrice());
            } else {
                orderdetail.setPrice(map.get(integer).getNowPrice());
            }
            orderdetail.setNumber(map.get(integer).getCount());
            bsi.updateGoodsNumber(map.get(integer).getCount(), new Integer(map.get(integer).getId()));
            bsi.insertOrderDetail(orderdetail);

        }
        out.println("<script language='javascript'>alert('请记住订单编号');</script>");
        response.sendRedirect("ClearCart");
        // out.println("<script language='javascript'>alert('请记住订单编号');window.location.href='ClearCart';</script>");

    }
}
